#include "GnSystemPCH.h"
#include "GnFile.h"


GnFile* GnFile::GetFile( gtchar* fileName, OpenMode openMode, gtuint bufferSize )
{
	GnAssert( msAsciiFileCreateFunc || msBinaryFileCreateFunc );
	if( !GnFile::IsExistFile( fileName ) )
		return NULL;

	FileCreateFunc* createFunc = msBinaryFileCreateFunc;
	if( openMode < READ_ACSII )
		createFunc = msAsciiFileCreateFunc;
	
	return createFunc( fileName, openMode, bufferSize );
}

GnFile::GnFile( gtchar* fileName, OpenMode openMode, gtuint bufferSize )
	: mOpenMode(openMode), mBufferSize(bufferSize)
{
	gtchar stringOpenMode[NUM_OPENMODE][3] =
	{
		"rb",
		"wb",
		"ab",
		"r",
		"w",
		"a"
	};
	FileOpen( fileName, stringOpenMode[openMode] );
}

GnFile::~GnFile(void)
{
	FileClose();
}

gsize GnFile::ReadBinary( void* pBuffer, gsize size )
{
	return fread( pBuffer, size, 1, mFile );
}

gsize GnFile::WriteBinary( const void * pBuffer, gsize size )
{
	return fwrite( pBuffer, size, 1, mFile );
}

gint GnFile::ReadAscii( const gtchar* format, va_list& args )
{
	//for (int i=1;i<amount;i++)
	//{
	//	val=va_arg(vl,int);
	//	greater=(greater>val)?greater:val;
	//}
	gint ret = GnTfsscanf( mFile, format, args );
	va_end( args );
	return ret;
}
gint GnFile::WriteAscii( const gtchar* format, va_list& args )
{
	gint ret = GnTvfprintf( mFile, format, args );
	va_end( args );
	return ret;
}